<?php

/*
Copyright 2007,2008 Benjamin V King

BH Widgets is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

BH Widgets is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with BH Widgets; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once('BH_Widget_Base.php');

class BH_Search_Widget extends BH_Controllable_Widget_Base {
	
	function widget($args) {
		extract($args);

		$title = $this->get_attribute('title');
		$button_text = $this->get_attribute('button-text');
		$image_url = $this->get_attribute('image-url');
		$search_text = $this->get_attribute('search-text');

		echo "$before_widget\n";
		echo $this->get_widget_start_tag($args);
		
		if (strlen($title) > 0)
			echo $before_title . $title . $after_title . "\n";
			$onfocus="";
			
		if ($s = wp_specialchars(get_query_var('s'), 1)) {
			
		} else {
			$s = $search_text;
			$onfocus = "onFocus=\"this.value=''\"";
		}

		echo "<form method=\"post\" action=\"/index.php\" name=\"searchForm\">\n";
		echo "<input type=\"text\" value=\"$s\" name=\"s\" id=\"s\" $onfocus />\n";
		
		if ($image_url) {
			echo "<input type=\"image\" src=\"". $image_url ."\" />";
		} else {
			echo "<a href=\"#\" onClick=\"searchForm.submit();\" >$button_text &raquo;</a>\n";
		}
		
		echo "</form>\n";
		
		echo $this->get_widget_end_tag($args);
		echo $after_widget ."\n";
	}

	function control() {
		if ( $this->get_request_param_value('submit') ) {
			$this->update_attribute_from_param('title');
			$this->update_attribute_from_param('button-text');
			$this->update_attribute_from_param('image-url');
			$this->update_attribute_from_param('search-text');
		}

		$title = $this->get_attribute('title');
		$style = $this->get_attribute('style');
		$button_text = $this->get_attribute('button-text');
		$image_url = $this->get_attribute('image-url');
		$search_text = $this->get_attribute('search-text');

		echo "<label>Title:</label><input name=\"" . $this->get_request_param_name('title') . "\" value=\"". $title ."\"/><br/>\n";
		echo "<label>Button Text:</label><input name=\"" . $this->get_request_param_name('button-text') . "\" value=\"". $button_text ."\"/><br/>\n";
		echo "<label>Image URL:</label><input name=\"" . $this->get_request_param_name('image-url') . "\" value=\"". $image_url ."\"/><br/>\n";
		echo "<label>Search Text:</label><input name=\"" . $this->get_request_param_name('search-text') . "\" value=\"". $search_text ."\"/>\n";
		echo "<input type=\"hidden\" id=\"" . $this->get_request_param_name('title') . "\" name=\"" . $this->get_request_param_name('submit') . "\" value=\"1\" />\n";
	}
}

bh_widgets_register_class('BH_Search_Widget', 380,150);
?>
